<?php
namespace app\admin\controller;
use app\admin\model\User as UserModel;

//用户管理类
class User extends Base
{
    /**
     * 用户列表 状态正常
     */
    public function lst()
    {
        //field 第二个参数为true为除去这些字段
        $list = UserModel::where('us_status',1)->field('us_password,us_status,us_black_aid,us_note,us_black_time',true)->order('us_create_time DESC')->paginate(10);

        $info['code'] = '200';
        $info['data'] = $list;

        echo displayJson($info);die;
    }

    /**
     * 获取用户详情
     */
    public function userDesc()
    {
        $uid = input('uid');

        $getRow = db('user',[],false)->where('us_id',$uid)->field('us_password',true)->find();

        if ($getRow['us_black_aid']) {
            $getAdmin = db('admin',[],false)->where('ad_id',$getRow['us_black_aid'])->find();

            $getRow['us_black_aid'] = $getAdmin['ad_account'];
        }

        if ($getRow) {
            $info['code'] = '200';
            $info['data'] = $getRow;
            echo displayJson($info);die;
        } else {
            $info['code']    = '400';
            $info['message'] = '获取失败';
            echo displayJson($info);die;
        }
    }

    /**
     * 向用户发送邮件
     */
    public function sendEmailToUser()
    {
        $content = input('content');
        $uid     = input('uid'); //用户id
        $aid     = input('aid'); //管理员id

        $content = htmlspecialchars($content);

        if (empty($content)) {
            $info['code']    = '400';
            $info['message'] = '发送内容不能为空';
            echo displayJson($info);die;
        }

        if (empty($uid) || empty($aid)) {
            $info['code']    = '401';
            $info['message'] = '重要参数缺失，系统自动退出';
            echo displayJson($info);die;
        }

        $getUser = db('user',[],false)->where('us_id',$uid)->where('us_status',1)->find();

        if (!$getUser) {
            $info['code']    = '400';
            $info['message'] = '该用户状态异常';
            echo displayJson($info);die;
        }

        $send_data = array(
            'to_email'   =>$getUser['us_email'],
            'content'    =>'【淘二货】--[来自管理员的回复]:<br/>'.$content
        );

        $send      = sendEmail($send_data);

        if (!$send) {
            $info['code']    = '400';
            $info['message'] = '发送失败';
            echo displayJson($info);die;
        } else {
            //存入邮件记录表
            $insert['el_aid']         = $aid;
            $insert['el_uid']         = $uid;
            $insert['el_content']     = $content;
            $insert['el_create_time'] = time();

            $log = db('email_log',[],false)->insert($insert);

            if ($log) {
                $info['code']    = '200';
                $info['message'] = '发送成功';
                echo displayJson($info);die;
            } else {
                $info['code']    = '400';
                $info['message'] = '邮件发送成功，记录失败';
                echo displayJson($info);die;
            }
        }
    }

    /**
     * 将用户拉入黑名单
     */
    public function pullBlack()
    {
        $uid  = input('uid');
        $aid  = input('aid');
        $note = htmlspecialchars(input('note'));

        if (empty($uid) || empty($aid)) {
            $info['code']    = '401';
            $info['message'] = '重要参数缺失，系统自动退出';
            echo displayJson($info);die;
        }

        if (empty($note)) {
            $info['code']    = '400';
            $info['message'] = '拉黑原因必须填写';
            echo displayJson($info);die;
        }

        $getUser = db('user',[],false)->where('us_id',$uid)->find();

        if ($getUser['us_status'] == 2) {
            $info['code']    = '400';
            $info['message'] = '该用户已列入黑名单,请刷新页面查看';
            echo displayJson($info);die;
        }

        $set['us_status']     = 2;
        $set['us_black_aid']  = $aid;
        $set['us_note']       = $note;
        $set['us_black_time'] = time();

        $res = db('user',[],false)->where('us_id',$uid)->update($set);

        if ($res) {
            //发送邮件告知用户
            $send_data = array(
                'to_email'   =>$getUser['us_email'],
                'content'    =>'【淘二货】您的账户：<span style="font-size: 20px;font-weight: bold;"> '.$getUser['us_email'].' </span>，已被管理员拉入黑名单。<br/>拉黑原因： '.$note.'<br/>如有疑问，请联系管理员QQ：1298025852'
            );

            $send      = sendEmail($send_data);

            if (!$send) {
                $info['code']    = '400';
                $info['message'] = '拉入黑名单成功,发送邮件失败';
                echo displayJson($info);die;
            } else {
                $info['code']    = '200';
                $info['message'] = '拉入黑名单成功';
                echo displayJson($info);die;
            }
        } else {
            $info['code']    = '400';
            $info['message'] = '拉入黑名单失败';
            echo displayJson($info);die;
        }
    }

    /**
     * 黑名单用户列表
     */
    public function userBlackLst()
    {
        $list = UserModel::where('us_status',2)->field('us_password,us_status',true)->order('us_black_time DESC')->paginate(10);

        $data = displayJson($list);

        $arr  = json_decode($data,true);

        $admin_list = db('admin',[],false)->column('ad_account','ad_id');

        $newArr = $arr['data'];

        foreach ($newArr as $key => $val) {
            $newArr[$key]['us_black_aid'] = $admin_list[$val['us_black_aid']];
        }

        $arr['data'] = $newArr;

        $info['code'] = '200';
        $info['data'] = $arr;

        echo displayJson($info);die;
    }

    /**
     * 将用户状态从黑名单改为正常
     */
    public function reductionUser()
    {
        $uid = input('uid');

        if (empty($uid)) {
            $info['code']    = '401';
            $info['message'] = '重要参数缺失，系统自动退出';
            echo displayJson($info);die;
        }

        $set['us_status']    = 1;
        $set['us_black_aid'] = NULL;
        $set['us_note']      = NULL;
        $set['us_black_time']= NULL;

        $res = db('user',[],false)->where('us_id',$uid)->update($set);

        if ($res) {
            //向用户发送邮件
            $user = db('user',[],false)->where('us_id',$uid)->find();
            $send_data = array(
                'to_email'   =>$user['us_email'],
                'content'    =>'【淘二货】您的账户：<span style="font-size: 20px;font-weight: bold;"> '.$user['us_email'].' </span>，状态现已恢复正常。<br/>如有疑问，请联系管理员QQ：1298025852'
            );

            $send      = sendEmail($send_data);

            if (!$send) {
                $info['code']    = '400';
                $info['message'] = '用户状态更新成功,邮件发送失败';
                echo displayJson($info);die;
            } else {
                $info['code']    = '200';
                $info['message'] = '用户状态更新成功';
                echo displayJson($info);die;
            }
        } else {
            $info['code']    = '400';
            $info['message'] = '用户状态更新失败';
            echo displayJson($info);die;
        }
    }






}
